/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author PhucAnh
 */
public class FillRectangular extends Shape{
    Point pointStart;
    Point pointFinish;
    
    int x;
    int y;
    int width;
    int height;
    
    public FillRectangular() {

    }

    public FillRectangular(Point pointStart, Point pointFinish, Color color, int basicStrok) {
        super(color, basicStrok);
        this.pointStart = pointStart;
        this.pointFinish = pointFinish;
        
        width = pointFinish.getX() - pointStart.getX();
        height = pointFinish.getY() - pointStart.getY();
        
//      Chinh sua vi tri hinh oval
        if(width <=0 && height <=0) {
            width = -width;
            height = -height;
            x = pointFinish.getX();
            y = pointFinish.getY();   
        } else if (width <=0 && height >0){
            width = -width;
            x = pointFinish.getX();
            y = pointStart.getY();            
        } else if (width >0 && height <=0){
            height = -height;
            x = pointStart.getX();
            y = pointFinish.getY();  
        } else{
            x = pointStart.getX();
            y = pointStart.getY(); 
        }
       
    }
    
    public void setPoint2(Point pointFinish){
        this.pointFinish = pointFinish;
        
        width = pointFinish.getX() - pointStart.getX();
        height = pointFinish.getY() - pointStart.getY();
        
//      Chinh sua vi tri hinh oval
        if(width <=0 && height <=0) {
            width = -width;
            height = -height;
            x = pointFinish.getX();
            y = pointFinish.getY();   
        } else if (width <=0 && height >0){
            width = -width;
            x = pointFinish.getX();
            y = pointStart.getY();            
        } else if (width >0 && height <=0){
            height = -height;
            x = pointStart.getX();
            y = pointFinish.getY();  
        } else{
            x = pointStart.getX();
            y = pointStart.getY(); 
        }
       
    
    }
    
    @Override
    public void drawShape(Graphics g2,BufferedImage img)
    {
        Graphics2D g= (Graphics2D)g2;
        g.setStroke(new BasicStroke(this.getBasicStroke()));
        g.setColor(this.getColor());
        g.fillRect(x, y, width, height);
    }
    @Override
    public void toMove(int m,int n)
    {
//        x +=m;
//        y +=n;
//        
//        pointFinish.toMove(m, n);
//        pointStart.toMove(m, n);
        pointStart.toMove(m, n);
        pointFinish.toMove(m, n);
        setPoint2(pointFinish);
    }
    
    public boolean isContain(Point p){
        if(pointFinish.getX()>=pointStart.getX() && pointFinish.getY()>=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()<pointStart.getX() && pointFinish.getY()<pointStart.getY()){
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else if (pointFinish.getX()>=pointStart.getX() && pointFinish.getY()<=pointStart.getY()){
            if(p.getX()>= pointStart.getX() && p.getX()<= pointFinish.getX() 
            && p.getY()>= pointFinish.getY() && p.getY()<= pointStart.getY()){
                return true;
            } else {
                return false;
            }
        } else {
            if(p.getX()>= pointFinish.getX() && p.getX()<= pointStart.getX() 
            && p.getY()>= pointStart.getY() && p.getY()<= pointFinish.getY()){
                return true;
            } else {
                return false;
            }
        }
    }
    
    public void replay(Graphics g,int time,Image a,Control.PlayBack.MyPanel1 a2){
        int x=pointFinish.getX()- pointStart.getX();
        int y=pointFinish.getY()- pointStart.getY();
        Graphics2D g2D= (Graphics2D)g;
        g2D.setStroke(new BasicStroke(this.getBasicStroke()));
        g.setColor(this.getColor());
        Image img = a;
        Point tmp=new Point();
        for(int i=0;i<=time;i++){
          g.drawImage(img,0, 0, a2);
          tmp.setX(x*i/time+pointStart.getX());
          tmp.setY(y*i/time+pointStart.getY());
          setPoint2(tmp);
          g.fillRect(this.x, this.y, width, height);
          try {
                Thread.sleep(time);
              } catch (InterruptedException ex) {
          Logger.getLogger(Curve.class.getName()).log(Level.SEVERE, null, ex);
              }
        }
    }
    
}
